package com.blog.wu.myblog.controller;

import com.blog.wu.myblog.base.BaseController;
import com.blog.wu.myblog.entity.Role;
import com.blog.wu.myblog.entity.UserInfo;
import com.blog.wu.myblog.service.RoleService;
import com.blog.wu.myblog.service.UserInfoService;
import com.blog.wu.myblog.util.PageBean;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-09-25 下午2:14
 * @Discription 用户信息controller
 */
@Controller
@RequestMapping("/back/user")
public class UserInfoController extends BaseController {


    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RoleService roleService;


    /**
     * 根据分页条件查询用户信息
     *
     * @param user
     * @param model
     * @param pageNo
     * @return
     */
//    @RequiresRoles("superAdmin")
    @RequestMapping("index")
    public String index(UserInfo user, Model model, Integer pageNo) {
        logger.info("开始分页用户查询...");
        try {
            PageBean<UserInfo> pageBean = userInfoService.findPage(user, pageNo);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("userInfo", user);
            return "/back/userinfo/userinfo_list";

        } catch (Exception e) {
            logger.info("分页用户查询出错了 【{}】", e.getMessage());
        } finally {
            logger.info("分页用户查询结束...");
        }
        return null;
    }

    /**
     * 添加用户信息
     *
     * @param user
     * @param attributes
     * @return
     */
    @RequestMapping("add")
    public String add(UserInfo user, @Param("roleArray") String roleArray, RedirectAttributes attributes) {
        logger.info("开始用户新增...");
        try {
            user.setRoles(getRoles(roleArray));
            boolean flag = userInfoService.saveUser(user);
            if (flag) {
                attributes.addAttribute("info", "添加用户信息成功");
            } else {
                attributes.addAttribute("info", "添加用户信息失败");
            }
            return "redirect:/back/user/index?page=1";

        } catch (Exception e) {
            logger.error("用户新增出错了 【{}】", e.getMessage());
        } finally {
            logger.info("用户新增结束...");
        }
        return null;
    }

    @RequestMapping("loadAdd")
    public String loadAdd(Model model) {
        List<Role> roleList = roleService.findList(new Role());
        model.addAttribute("roleList", roleList);
        return "/back/userinfo/userinfo_add";
    }

    /**
     * 根据编号加载修改页面
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("loadUpdate")
    public String loadUpdate(UserInfo user, Model model, @ModelAttribute("info") String info) {
        UserInfo userInfo = userInfoService.get(user);
        List<Role> roleList = roleService.findList(new Role());
        List<String> roleIds = userInfoService.findUserRole(user);
        model.addAttribute("roleIds", roleIds);
        model.addAttribute("roleList", roleList);
        model.addAttribute("user", userInfo);
        return "/back/userinfo/userinfo_update";
    }

    private List<Role> getRoles(String roleArray) {
        if (roleArray == null) {
            return null;
        }
        List<Role> list = new ArrayList<>();
        String s = ",";
        if (roleArray.contains(s)) {
            String[] roles = roleArray.split(",");
            for (String id : roles) {
                Role role = new Role();
                role.setId(id);
                list.add(role);
            }
        } else {
            Role role = new Role();
            role.setId(roleArray);
            list.add(role);
        }
        return list;
    }

    /**
     * 根据编号修改用户信息
     *
     * @param user
     * @param redirectAttributes
     * @return
     */
    @RequestMapping("update")
    public String update(UserInfo user, @Param("roleArray") String roleArray, RedirectAttributes redirectAttributes) {
        logger.info("开始用户更新...");
        try {
            user.setRoles(getRoles(roleArray));
            boolean flag = userInfoService.updateUser(user);
            if (flag) {
                redirectAttributes.addAttribute("info", "修改用户信息成功");
            } else {
                redirectAttributes.addAttribute("info", "修改用户信息失败");
            }
            redirectAttributes.addAttribute("id", user.getId());
            return "redirect:/back/user/loadUpdate";

        } catch (Exception e) {
            logger.info("用户更新出错了 【{}】", e.getMessage());
        } finally {
            logger.info("用户更新结束...");
        }

        return null;
    }

    /**
     * 根据编号修改用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("delete")
    public String delete(UserInfo user) {
        userInfoService.deleteUser(user);
        return "redirect:/back/user/index?pageNo=1";
    }



}
